/*
 * ============================================================================
 *                   GNU Lesser General Public License
 * ============================================================================
 *
 *
 *
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 * 
 *
 *
 */
package rad.framework.jmx;

import java.util.List;

import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.jboss.mx.util.MBeanProxy;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Startup;
import org.jboss.seam.annotations.Unwrap;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
import org.jboss.seam.log.Log;

/**
 * Register, create, and manage an MBean.
 * 
 * @author boudyacho
 */
@Scope(ScopeType.APPLICATION)
@AutoCreate
@Startup
@BypassInterceptors
public class ManagedBean {

	@Logger
	private Log log;

	private String objectName;

	private ObjectName _objectName;

	private Class<?> type;

	private Class<?> implementation;

	private Object mbean;

	private String agentId;

	private MBeanServer server;

	// TODO mbean persistence ???

	@Create
	public void create() throws Exception {
		List<MBeanServer> servers = MBeanServerFactory.findMBeanServer(agentId);
		server = servers.get(0);
		
		_objectName = new ObjectName(objectName);
		mbean = MBeanProxy.create(implementation, type, _objectName, server);

		log.debug("Created: #0", this);
	}

	@Destroy
	public void destroy() throws Exception {
		log.debug("Destroying: #0", this);
		server.unregisterMBean(_objectName);
	}

	@Unwrap
	public Object getMBean() {
		return mbean;
	}

	// ---------------------------------------------------------------

	public String getObjectName() {
		return objectName;
	}

	public void setObjectName(String objectName) {
		this.objectName = objectName;
	}

	public Class<?> getType() {
		return type;
	}

	public void setType(Class<?> type) {
		this.type = type;
	}

	public Class<?> getImplementation() {
		return implementation;
	}

	public void setImplementation(Class<?> implementation) {
		this.implementation = implementation;
	}

	public String getAgentId() {
		return agentId;
	}

	public void setAgentId(String agentId) {
		this.agentId = agentId;
	}

	public String toString() {
		return ToStringBuilder.reflectionToString(this,
				ToStringStyle.MULTI_LINE_STYLE);
	}
}
